//-----------------------------------------------------------------------
// <copyright file="HelpDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class HelpDAOBase
    {
        #region Common methods
        public virtual Help CreateHelpFromReader(IDataReader reader)
        {
            Help item = new Help();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("HelpId"))) item.HelpId = (long)reader["HelpId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatId"))) item.CatId = (long)reader["CatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) item.Content = (string)reader["Content"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThumnailImage"))) item.ThumnailImage = (string)reader["ThumnailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("DetailImage"))) item.DetailImage = (string)reader["DetailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateHelpFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateHelp methods
            
        public virtual void CreateHelp(Help help)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpInsert");
                
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, help.CatId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, help.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, help.Summary);
                database.AddInParameter(dbCommand, "@Content", DbType.String, help.Content);
                database.AddInParameter(dbCommand, "@ThumnailImage", DbType.String, help.ThumnailImage);
                database.AddInParameter(dbCommand, "@DetailImage", DbType.String, help.DetailImage);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, help.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, help.UpdatedDate);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, help.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, help.Enable);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, help.UserId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, help.StatusId);
                database.AddOutParameter(dbCommand, "@HelpId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                help.HelpId = (long)database.GetParameterValue(dbCommand, "@HelpId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateHelpException, ex);
            }
        }

        #endregion

        #region UpdateHelp methods
        
        public virtual void UpdateHelp(Help help)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpUpdate");            
                
                database.AddInParameter(dbCommand, "@HelpId", DbType.Int64, help.HelpId);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, help.CatId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, help.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, help.Summary);
                database.AddInParameter(dbCommand, "@Content", DbType.String, help.Content);
                database.AddInParameter(dbCommand, "@ThumnailImage", DbType.String, help.ThumnailImage);
                database.AddInParameter(dbCommand, "@DetailImage", DbType.String, help.DetailImage);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, help.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, help.UpdatedDate);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, help.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, help.Enable);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, help.UserId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, help.StatusId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateHelpException, ex);
            }
        }
        
        #endregion

        #region DeleteHelp methods
        public virtual void DeleteHelp(long helpId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpDelete");
                
                database.AddInParameter(dbCommand, "@HelpId", DbType.Int64, helpId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteHelpException, ex);
            }
        }

        #endregion

        #region GetHelp methods
        
        public virtual Help GetHelp(long helpId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpGet");
                
                database.AddInParameter(dbCommand, "@HelpId", DbType.Int64, helpId);
                
                Help help =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        help = CreateHelpFromReader(reader);
                        reader.Close();
                    }
                }
                return help;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpException, ex);
            }
        }
        
        #endregion

        #region GetHelpList methods
        public virtual HelpCollection GetHelpList(HelpColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HelpCollection helpCollection = new HelpCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Help help = CreateHelpFromReader(reader);
                        helpCollection.Add(help);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return helpCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpListException, ex);
            }
        }
        
        public virtual HelpCollection GetHelpList(HelpColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetHelpList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetHelpList methods
        public virtual HelpCollection GetHelpListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHelpGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HelpCollection helpCollection = new HelpCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Help help = CreateHelpFromReader(reader);
                        helpCollection.Add(help);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return helpCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetHelpListException, ex);
            }
        }
        
        public virtual HelpCollection GetHelpListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetHelpListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}